import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({showSpinner:false})

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 设置全局前置守卫函数
router.beforeEach((to,from,next)=>{
  NProgress.start( )
  // 获取token
  var token = localStorage.getItem('token')

  // 判断是否是跳到订单页 或者 我的 页面
  if(to.path == '/index/order' || to.path == '/index/mine' || to.path == '/details'){
    if(token){
      next()
    }else{
      console.log(111);
      next('/login')
      // router.push('/login')
    }
  }else{
    next()
  }
})

// 设置全局后置守卫函数
router.afterEach((to,from)=>{
  NProgress.done( )
})

export default router
